//
//  WCNZhumuManager.m
//  WCN
//
//  Created by 陈武琦 on 16/5/31.
//  Copyright © 2016年 ShanghaiCize Trade And Business Co., Ltd. All rights reserved.
//

static NSString *kAPI_KEY = @"BB056CF2009942C24D2CC4C5A3953CD5";
static NSString *kAPI_SECRET = @"1DFACCD81D2062D52ED05A281A31D25A";
//#define kAPI_KEY  @"O8czzSQBEWkq0sHDk8PixUc9FdY27"
//#define kAPI_SECRET @"YgAfm0VURjiCeYgAfm0V=="
//账户登录类型:1-Zcode, 2-Mobile, 3-Email
#define kLOGIN_TYPE 3 //代表email类型

#import "WCNZhumuManager.h"
#import "CallCenter.h"
#import "UIImage+Additions.h"
#import "UIColor+Additions.h"


@implementation WCNZhumuManager
+ (instancetype)shareManager
{
    static WCNZhumuManager *zhumuInstance = nil;
    //block
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        zhumuInstance = [[WCNZhumuManager alloc] initPrivate];
    });
    return zhumuInstance;
}

- (instancetype)initPrivate
{
    self = [super init];
    if (self) {

//#if DEBUG
//        kAPI_KEY = @"O8czzSQBEWkq0sHDk8PixUc9FdY27";
//        kAPI_SECRET = @"YgAfm0VURjiCeYgAfm0V==";
//#else
//        kAPI_KEY = @"BB056CF2009942C24D2CC4C5A3953CD5";
//        kAPI_SECRET = @"1DFACCD81D2062D52ED05A281A31D25A";
//#endif
    }
    return self;
}
//获取瞩目帐号信息
- (void)getZhumuDataWithLoginNumber:(NSString *)number completionBlock:(void (^)(id obj))block {
    
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:[UIApplication sharedApplication].keyWindow];
    AFHTTPSessionManager *sessionManager = [AFHTTPSessionManager manager];
    NSDictionary *parameters = @{@"api_key":kAPI_KEY, @"api_secret":kAPI_SECRET, @"logintype":@3,@"loginname":number};
    [sessionManager POST:@"https://api.zhumu.me/v1/user/get" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *result = responseObject;
        block(result[@"pmi"]);
        [hud hide:YES];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        block(nil);
        [hud hide:YES];
    }];
}

//开始会议
- (void)creatMeetingWithNumber:(NSString *)number vc:(UIViewController *)vc
{
    
    MBProgressHUD *hud = [[WCPopupLayerManager shareMg] showSimple:[UIApplication sharedApplication].keyWindow];
    
    NSString *loginNumber = number;
//    __block WCNCreateMeetingVC *weakself = self;
    __block UIView *waitView = hud;
    __block RequestMeeting *info = [[RequestMeeting alloc] initWithKey:kAPI_KEY secret:kAPI_SECRET type:kLOGIN_TYPE name:loginNumber];
    //即时会议: Instant_CallType
    //预约会议: Scheduled_CallType
    [[CallCenter sharedInsance] startCallWithType:Scheduled_CallType info:info delegate:self success:^(Meeting *meeting) {
        
        //success:
        //NSLog(@"%@",meeting);
        [waitView setHidden:YES];
        
    } failure:^(NSString *error) {
        
        [waitView setHidden:YES];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hint", @"") message:[NSString stringWithFormat:NSLocalizedString(@"Failure: %@", @""),error] delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
        [alert show];
        
    }];
}

//加入会议
- (void)joinMeeting:(NSString *)meetID vc:(UIViewController *)vc {
    
    Account *account =[[WCDefaultManager shareMg] account];
    NSString *diplayName = account.name;
    ZhumuSDKMeetingService *ms = [[ZhumuSDK sharedSDK] getMeetingService];
    if (ms)
    {
        //__block WCNJoinMeetingVC *weakself = self;
        __block UIView *waitView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(kScreenWidth/2 - 20, kScreenHeight/2 -20, 40, 40)];
        __block MeetingInfo *info = [[MeetingInfo alloc] initWithName:diplayName meetingID:meetID];
        [[CallCenter sharedInsance] startCallWithType:Join_CallType info:info delegate:vc success:^(Meeting *meeting) {
            
            [[WCDefaultManager shareMg] saveLastMeetingAccouts:meetID];
            [waitView setHidden:YES];
        } failure:^(NSString *error) {
            
            [waitView setHidden:YES];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Hint", @"") message:[NSString stringWithFormat:NSLocalizedString(@"Failure: %@", @""),error] delegate:self cancelButtonTitle:NSLocalizedString(@"OK", @"") otherButtonTitles:nil, nil];
            [alert show];
            
        }];
    }
}

@end
